package com.ellricky.baselib.ui.cla

import android.content.Intent
import android.view.View
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.BaseConstant.RESULT_CODE_CREATE
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.util.jump
import com.ellricky.baselib.widget.calendar.WeekDateEntity
import com.ellricky.baselib.widget.calendar.WeekDateView
import kotlinx.android.synthetic.main.base_activity_class_manage.*

class ClassManageActivity : BaseToolBarActivity<Void, BasePresenter<Void>>(),
    WeekDateView.OnSelectListener {

    private lateinit var fragment: ClassManageFragment
    private var curWeekDate: String? = null

    private val type: String by lazy {
        intent.extras?.getString(BaseConstant.KEY_TYPE) ?: BaseConstant.TYPE_DEFAULT
    }

    override fun getContentLayout() = R.layout.base_activity_class_manage

    override fun initUIView() {
        super.initUIView()
        if (type == BaseConstant.TYPE_EVALUATE) {
            mTvTitle.text = "评估列表"
        } else {
            mTvTitle.text = getString(R.string.base_class_manage)
            val isTc = LocalCacheUtil.isTcRole()
            if (!isTc) {
                mBtnRight.visibility = View.VISIBLE
                mBtnRight.text = getString(R.string.base_class_create)
            }
        }

        initPageView()
    }

    private fun initPageView() {
        weekDateView.visibility = View.VISIBLE
        fragment = ClassManageFragment.newInstance(false)
        supportFragmentManager.beginTransaction()
            .replace(R.id.frgContainer, fragment)
            .commit()
        fragment.classRoomType = type
        weekDateView.setOnSelectListener(this)
        weekDateView.getData("", false)
    }


    override fun onSelected(position: Int, dateEntity: WeekDateEntity?) {
        curWeekDate = dateEntity?.date
        fragment.weekDate = curWeekDate
        fragment.loadClassRooms()
    }

    override fun onChangeWeek(position: Int) {

    }

    fun showTips() {
        if (type == BaseConstant.TYPE_EVALUATE) {
            tv_tips.visibility = View.VISIBLE
        }
    }

    override fun onClickBtnRight() {
        super.onClickBtnRight()
        //课程顾问只能创建体验课
        if (LocalCacheUtil.getCardType() == BaseConstant.CARD_CONSULTANT) {
            jump<ClassExperienceCreateActivity> {}
        } else {
            jump<ClassCreateActivity> {}
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (resultCode) {
            RESULT_CODE_CREATE -> fragment.onActivityResult(requestCode, resultCode, data)
        }
    }
}
